const express = require('express')
const router = express.Router()
const path = require('path')
const RoleModel = require(path.join(process.cwd(), '/models/RoleModel'))
const RouterModel = require(path.join(process.cwd(), '/models/RouterModel'))
const checkTokenMiddleware = require(path.join(process.cwd(), '/middlewares/checkTokenMiddleware'))

// 根据角色 id 获取路由
router.get('/router/:rId', checkTokenMiddleware, async (req, res) => {
  const rId = req.params.rId
  try {
    const data = await RoleModel.findOne({ role_id: rId })
    if (data) {
      const resData = await RouterModel.find({ id: { $in: data.router_ids } }).sort({ id: 1 })
      return res.sendResult(resData, 200, '获取路由成功')
    }
    return res.sendResult(null, 400, '角色 ID 错误')
  } catch (error) {
    return res.sendResult(error.message, 400, '获取路由失败')
  }
})

module.exports = router
